/**
 * jims
 */
package com.jims.master.modules.doctor.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.PrintPdfUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.doctor.dao.ClinicMasterDao;
import com.jims.master.modules.doctor.dao.ClinicRecordDao;
import com.jims.master.modules.doctor.dao.HisDiagnosisDao;
import com.jims.master.modules.doctor.dao.PatMasterIndexDao;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.ClinicRecord;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.common.utils.LongTextToString;
import com.jims.master.modules.sys.dao.CompanyDao;
import com.jims.master.modules.sys.dao.OrgDeptDao;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.DictUtils;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Service
 * @author 陈涛
 * @version 2017-03-16
 */
@Service
@Transactional(readOnly = true)
public class ClinicRecordService extends CrudService<ClinicRecordDao, ClinicRecord> {
	@Autowired
	private ClinicRecordDao clinicRecordDao;
	@Autowired
	private ClinicMasterDao clinicMasterDao;
	@Autowired
	private  ClinicMasterService clinicMasterService;
	@Autowired
	private PatMasterIndexDao patMasterIndexDao;
	@Autowired
	private CompanyDao companyDao;
	@Autowired
	private OrgDeptDao orgDeptDao;
	@Autowired
	private HisDiagnosisDao hisDiagnosisDao;
	public ClinicRecord get(String id) {
		return super.get(id);
	}
	
	public List<ClinicRecord> findList(ClinicRecord clinicRecord) {
		return super.findList(clinicRecord);
	}
	
	public Page<ClinicRecord> findPage(Page<ClinicRecord> page, ClinicRecord clinicRecord) {
		return super.findPage(page, clinicRecord);
	}
	
	@Transactional(readOnly = false)
	public int save(ClinicRecord clinicRecord) {
		return super.save(clinicRecord);
	}
	
	@Transactional(readOnly = false)
	public int delete(ClinicRecord clinicRecord) {
		return super.delete(clinicRecord);
	}

	/**
	 * 病人病例页面
	 * @param clinicId
	 * @author 陈涛
	 * @return
	 */
	public ClinicRecord getRecordByClinicId(String clinicId){
		return  clinicRecordDao.getRecordByClinicId(clinicId,"");
	}

	@Transactional(readOnly = false)
	public int saveClinicRecord(ClinicRecord clinicRecord){
		int i = 0 ;
        User user= UserUtils.getUser();
		ClinicMaster clinicMaster = new ClinicMaster();
		String doctorId=clinicMasterService.get(clinicRecord.getClinicId()).getDoctorId();
		clinicMaster.setClinicType("1");
		if(StringUtils.isBlank(doctorId)){
			clinicMaster.setDoctorId(user.getId());
		}
		clinicMaster.setId(clinicRecord.getClinicId());
		this.save(clinicRecord);
		//修改clinic_master挂号状态
		i = clinicMasterService.updateClinicType(clinicMaster);
		return i ;
	}

	@Transactional(readOnly = false)
	public int saveRecord(ClinicRecord clinicRecord){
		int i = 0 ;
		String id = "";
		User user= UserUtils.getUser();
		ClinicMaster clinicMaster = new ClinicMaster();
		String doctorId=clinicMasterService.get(clinicRecord.getClinicId()).getDoctorId();
		clinicMaster.setClinicType("1");
		if(StringUtils.isBlank(doctorId)){
			clinicMaster.setDoctorId(user.getId());
		}
		clinicMaster.setId(clinicRecord.getClinicId());
		ClinicRecord clinicRecord1 = clinicRecordDao.getRecordByClinicId(clinicRecord.getClinicId(),clinicRecord.getPatientId());
		if(clinicRecord1 !=null){
			clinicRecord.setId(clinicRecord1.getId());
		}
		this.save(clinicRecord);
		//修改clinic_master挂号状态
		i = clinicMasterService.updateClinicType(clinicMaster);
		return i ;
	}


	/**
	 * 病人病例打印
	 * @param ids
	 * @return String
	 * @author 樊多
	 */
	public String printRegisit(String ids,String clinicId,String patientId,String type){
		User user = UserUtils.getUser();
		String [] id = ids.split(",");
		String printPath = "";
		for(int i = 0;i<id.length;i++){
			String hospital = companyDao.get(user.getOrgId()).getLinkMan();
			ClinicRecord clinicRecord=clinicRecordDao.get(id[i]);
			ClinicMaster clinicMaster=clinicMasterDao.get(clinicId);
			PatMasterIndex patMasterIndex= patMasterIndexDao.get(patientId);
			List<BaseDto> lists=hisDiagnosisDao.findIcdNameByClinicId(clinicId);
			String icdName="";
			for(BaseDto baseDto:lists){
				icdName=icdName+(String)baseDto.get("icdName")+",";
			}
			Map map = new HashMap();
			map.put("hospital",user.getCompany().getOrgName());
			map.put("name",patMasterIndex.getName());//患者姓名
			map.put("age",patMasterIndex.getAge());//患者年龄
			String sex = DictUtils.getDictLabels(patMasterIndex.getSex(), "SEX_DICT", "");
			map.put("sex",sex);
			String nation=DictUtils.getDictLabel(patMasterIndex.getNation(),"NATION_DICT","");
			map.put("nation",nation);//名族
			String marriage=DictUtils.getDictLabel(patMasterIndex.getMarriage(),"MARRIAGE_DICT","");
			map.put("marriage", marriage);//婚姻状态
			map.put("clinicNo",clinicMaster.getClinicNo());//门诊号
			map.put("address",patMasterIndex.getAddress());//住址
			map.put("icdName",icdName);//诊断
			map.put("suggestion",LongTextToString.unescape(clinicRecord.getSuggestion()));//意见，
			map.put("dispose",LongTextToString.unescape(clinicRecord.getDispose()));//处理
			map.put("userName",user.getName());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			map.put("visitDate",sdf.format(clinicMaster.getVisitDate()));//就诊时间
			OrgDept orgDept=orgDeptDao.get(clinicMaster.getDeptId());
			map.put("deptName",orgDept.getDeptName());//就诊科室
			map.put("drugAllergyHistory",LongTextToString.unescape(clinicRecord.getDrugAllergyHistory()));//药物过敏史
			map.put("zhuSu",LongTextToString.unescape(clinicRecord.getZhusu()));//主诉
			map.put("xianbingshi",LongTextToString.unescape(clinicRecord.getXianbingshi()));
			map.put("jiwangshi",LongTextToString.unescape(clinicRecord.getJiwangshi()));
			map.put("allergyHistory",LongTextToString.unescape(clinicRecord.getAllergyHistory()));
			map.put("chaTi",LongTextToString.unescape(clinicRecord.getTigejiancha()));
			Calendar calendar =Calendar.getInstance();
			calendar.setTime(new Date());
			int year = calendar.get(Calendar.YEAR);
			int month = calendar.get(Calendar.MONTH)+1;
			int day=calendar.get(Calendar.DAY_OF_MONTH);
			map.put("menzhen",year+"-"+month+"-"+day);
			map.put("zhenduan",year+"年"+month+"月"+day+"日");
			List list=new ArrayList();
			list.add("hello");
			if(type.equals("1")){//门诊
				printPath = PrintPdfUtils.previewPdf(user.getId(), "his/recordPrint.jasper", map, list, "recordPrint");
			}
			else{
				printPath = PrintPdfUtils.previewPdf(user.getId(), "his/diagnosisProve.jasper", map, list, "diagnosisProve");
			}

		}
		return printPath;
	}
	
}